package pers.myz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import pers.myz.common.QueryPageParam;
import pers.myz.common.Result;
import pers.myz.entity.SnsBlogComment;
import pers.myz.entity.SnsFriend;
import pers.myz.entity.SnsTestQuestion;
import pers.myz.mapper.SnsTestQuestionMapper;
import pers.myz.service.SnsBlogCommentService;
import pers.myz.service.SnsTestQuestionService;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzw
 * @since 2023-07-11
 */
@RestController
@RequestMapping("/sns-test-question")
public class SnsTestQuestionController {

    @Autowired
    private SnsTestQuestionService snsTestQuestionService;
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody SnsTestQuestion testQuestion){
        return snsTestQuestionService.save(testQuestion)?Result.suc():Result.fail();
    }

    @GetMapping("/ifCorrect")
    public Result ifFriend(@RequestParam Integer answer){
        List list = snsTestQuestionService.lambdaQuery().eq(SnsTestQuestion::getAnswer,answer).list();
        if(list.size()>0){
            SnsTestQuestion question1 = (SnsTestQuestion)list.get(0);
            HashMap res = new HashMap();
            res.put("question",question1);
            return Result.suc(res);
        }else{
            return Result.fail();
        }
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String testId = (String)param.get("testId");

        Page<SnsTestQuestion> page = new Page();
        page.setCurrent(query.getQuesNum());
        page.setSize(query.getQuesSize());

        LambdaQueryWrapper<SnsTestQuestion> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(testId) && !"null".equals(testId)){
            lambdaQueryWrapper.eq(SnsTestQuestion::getTestId,testId);
        }
        IPage result = snsTestQuestionService.pageC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

}
